using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace SermonEditor
{
    public partial class frmMain : Form
    {
        public string LastFileName;

        public frmMain()
        {
            InitializeComponent();
        }

        public void SetFormTitle(string Title)
        {
            frmMain.ActiveForm.Text = "Sermon Editor V.2.0 " + Title;
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditPreferences dlg = new EditPreferences();
            dlg.ShowDialog();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            grpMsgSettings.Enabled = false;
            LastFileName = "";
            Settings.LoadSettings();
        }

        private void quitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void currentFileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void lstMessages_SelectedIndexChanged(object sender, EventArgs e)
        {
            grpMsgSettings.Enabled = true;

            if (lstMessages.SelectedItem == null) return;
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;
            txtMessage.Text = rr.title;

            FindPreacher();
            FindSubCategory();

            msgCal.SelectionStart = DateTime.Parse(rr.date);
            msgCal.SelectionEnd = DateTime.Parse(rr.date);
            lblFileName.Text = rr.file;
        }

        public void FindPreacher()
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            int x;
            for (x = 0; x < cmbSpeaker.Items.Count; x++)
            {
                DataRowView drvPre = (DataRowView)cmbSpeaker.Items[x];
                NewDataSet.PreachersRow pr = (NewDataSet.PreachersRow)drvPre.Row;
                if (rr.PreacherIdx != pr.ID) continue;
                cmbSpeaker.SelectedIndex = x;
            }
        }

        public void FindSubCategory()
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            int x;
            for (x = 0; x < cmbSubCategory.Items.Count; x++)
            {
                DataRowView drvSub = (DataRowView)cmbSubCategory.Items[x];
                NewDataSet.SubcategoriesRow sr = (NewDataSet.SubcategoriesRow)drvSub.Row;
                if (rr.PreacherIdx != sr.ID) continue;
                cmbSubCategory.SelectedIndex = x;
            }
        }

        private void txtMessage_TextChanged(object sender, EventArgs e)
        {
        }

        private void txtMessage_Leave(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;
            if (rr.title == txtMessage.Text) return; // Nothing has changed...
            rr.title = txtMessage.Text;
            lstMessages.DataSource = dsRecords;
        }

        private void cmbSpeaker_Leave(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            drv = (DataRowView)cmbSpeaker.SelectedItem;
            NewDataSet.PreachersRow pr = (NewDataSet.PreachersRow)drv.Row;
            rr.PreacherIdx = pr.ID;
        }

        private void msgCal_Leave(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            DateTime NewDate = msgCal.SelectionStart;
            rr.date = NewDate.ToShortDateString();
        }

        private void cmbSubCategory_Leave(object sender, EventArgs e)
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            drv = (DataRowView)cmbSubCategory.SelectedItem;
            NewDataSet.SubcategoriesRow sr = (NewDataSet.SubcategoriesRow)drv.Row;
            rr.subcategory = sr.ID;
        }

        private void lnkDelete_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            DataRowView drv = (DataRowView)lstMessages.SelectedItem;
            NewDataSet.RecordingsRow rr = (NewDataSet.RecordingsRow)drv.Row;

            dsRecords.Recordings.Rows.Remove(rr);

            lstMessages.DataSource = dsRecords;
            lstMessages.SelectedIndex = -1;
            grpMsgSettings.Enabled = false;
        }

        private void loadXMLFromServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Title = "Select Recordings .XML file";
            dlg.Filter = "XML File|*.xml";
            if (dlg.ShowDialog() != DialogResult.OK) return; // Nothing to do here..
            LastFileName = dlg.FileName;
            dsRecords = new NewDataSet();
            dsRecords.ReadXml(LastFileName);
            lstMessages.DataSource = dsRecords;
            cmbSpeaker.DataSource = dsRecords;
            cmbSubCategory.DataSource = dsRecords;
        }

        private void saveXMLToServerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (LastFileName == "")
            {
                saveAsXMLToFileToolStripMenuItem_Click(sender, e);
                return;
            }

            dsRecords.WriteXml(LastFileName);
        }

        private void saveAsXMLToFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Select Recordings .XML file to save to...";
            dlg.Filter = "XML File|*.xml";
            if (dlg.ShowDialog() != DialogResult.OK) return; // Nothing to do here..
            LastFileName = dlg.FileName;
            saveXMLToServerToolStripMenuItem_Click(sender, e);
        }

        private void lnkAdd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            NewDataSet.RecordingsRow rr = dsRecords.Recordings.NewRecordingsRow();

            rr.file = "";
            rr.title = "TDB";
            rr.PreacherIdx = "0";
            rr.subcategory = "0";
            rr.date = DateTime.Today.ToShortDateString();
            dsRecords.Recordings.Rows.Add(rr);

            lstMessages.DataSource = dsRecords;
            lstMessages.SelectedIndex = lstMessages.Items.Count - 1;
        }

        private void loadXMLFromFTPToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ftpManager ftp = new ftpManager();
            string xmlStr = ftp.ReadAudioDataFile();
            if (xmlStr.Trim() == "")
            {
                MessageBox.Show("File not found or is empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.LoadXml(xmlStr);
            }
            catch(Exception)
            {
                MessageBox.Show("File not a valid XML file", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string tempFile = Path.GetTempFileName();
            File.WriteAllText(tempFile, xmlStr);
            dsRecords.ReadXml(tempFile);
            lstMessages.SelectedIndex = 0;
        }
    }

}